package com.ai.zhly.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import com.ai.dmc.index.common.InitParam;
import com.ai.dmc.index.common.bean.AreaBean;
import com.ai.dmc.index.common.cache.AreaCache;
import com.ai.dmc.index.common.cache.DataTimeCache;
import com.ai.dmc.index.common.util.DateUtil;
import com.ai.zhly.service.ICommonService;


/**
 * @Description : area任务
 * 
 * @author : wucm
 * @date : 2015年8月28日 上午11:16:10
 * @version V1.0
 * 
 */
public class AreaTask {
	private static Logger logger = Logger.getLogger(AreaTask.class);
	private static boolean executeFlagArea = false;// 防止多次执行
	private String tableName = "dim_table";
	
	private ICommonService commonService;
	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public void execute() {
		if (executeFlagArea) {
			return;
		}

		// 检查数据是否有更新
		if (!checkTime()) {
			return;
		}

		executeFlagArea = true;

		logger.debug("area任务开始时间："
				+ DateUtil.getInstance().getToday("yyyy-MM-dd hh:mm:ss"));

		// 加载area缓存
		loadAreaCache();

		logger.debug("area任务结束时间："
				+ DateUtil.getInstance().getToday("yyyy-MM-dd hh:mm:ss"));
		executeFlagArea = false;
	}

	/**
	 * 
	 * @Description : 检查数据是否有更新
	 * 
	 * @return boolean
	 * 
	 * @author : wucm
	 * @date : 2015年11月24日 下午7:05:10
	 * 
	 */
	private boolean checkTime() {
		boolean flag = true;

		String curTime = (String) DataTimeCache.dataTimeCache.get(tableName);

		String preTime = AreaCache.cacheTime;

		if (null != curTime && curTime.compareTo(preTime) <= 0) {
			flag = false;
		}

		return flag;
	}

	/**
	 * 
	 * @Description : 加载area缓存
	 * 
	 * @return JSONObject
	 * 
	 * @author : wucm
	 * @date : 2015年11月8日 下午4:28:13
	 * 
	 */
	private void loadAreaCache() {

		ConcurrentHashMap<String, Map<String, AreaBean>> map = new ConcurrentHashMap<String, Map<String, AreaBean>>();

//		CommonDB commonDB = new CommonDB(database, false);
		// 地市
		Map<String, AreaBean> cityMap = new HashMap<String, AreaBean>();
		// 区县
		Map<String, AreaBean> countyMap = new HashMap<String, AreaBean>();
		// 区域
		Map<String, AreaBean> regionMap = new HashMap<String, AreaBean>();
		// 子区域
		Map<String, AreaBean> subRegionMap = new HashMap<String, AreaBean>();
		
		Map<String, List<AreaBean>> allAreaMap= commonService._queryArea_();
		List<AreaBean> cityList = allAreaMap.get(AreaBean.TYPE_CITY);
		List<AreaBean> countyList =allAreaMap.get(AreaBean.TYPE_COUNTY);
		List<AreaBean> regionList = allAreaMap.get(AreaBean.TYPE_REGION);
		List<AreaBean> subRegionList = allAreaMap.get(AreaBean.TYPE_SUB_REGION);
//		try {
//			// 开启事务
//			commonDB.startDB();
//			List<AreaBean> cityList = commonDB.queryAreaCity();
//			List<AreaBean> countyList = commonDB.queryAreaCounty();
//			List<AreaBean> regionList = commonDB.queryAreaRegion();
//			List<AreaBean> subRegionList = commonDB.queryAreaSubRegion();
//
//			// 地市
			if (null != cityList) {
				for (AreaBean ab : cityList) {
					cityMap.put(ab.getAreaId(), ab);
				}
			}
			// 区县
			if (null != countyList) {
				for (AreaBean ab : countyList) {
					countyMap.put(ab.getAreaId(), ab);
				}
			}
			// 区域
			if (null != regionList) {
				for (AreaBean ab : regionList) {
					regionMap.put(ab.getAreaId(), ab);
				}
			}
			// 子区域
			if (null != subRegionList) {
				for (AreaBean ab : subRegionList) {
					subRegionMap.put(ab.getAreaId(), ab);
				}
			}
//		} catch (Exception e) {
//			logger.error(e.toString());
//		} finally {
//			try {
//				// 结束事务
//				commonDB.endDB();
//			} catch (Exception e) {
//				logger.error(e.toString());
//			}
//		}

		// 全部
		Map<String, AreaBean> allMap = new HashMap<String, AreaBean>();
		allMap.putAll(cityMap);
		allMap.putAll(countyMap);
		allMap.putAll(regionMap);
		allMap.putAll(subRegionMap);
		// 全部加入全省
		AreaBean prov = new AreaBean();
		prov.setAreaId(InitParam.DEPLOY_PROVINCE_ID);
		prov.setAreaName(InitParam.DEPLOY_PROVINCE_NAME);
		allMap.put(InitParam.DEPLOY_PROVINCE_ID, prov);

		map.put(AreaBean.TYPE_ALL, allMap);
		map.put(AreaBean.TYPE_CITY, cityMap);
		map.put(AreaBean.TYPE_COUNTY, countyMap);
		map.put(AreaBean.TYPE_REGION, regionMap);
		map.put(AreaBean.TYPE_SUB_REGION, subRegionMap);
		
		AreaCache.areaCache.clear();
		// 设置为缓存
		AreaCache.areaCache = map;

		// 设置缓存刷新时间
		String cacheTime = DateUtil.getInstance()
				.getToday("yyyy-MM-dd HH:mm:ss");
		AreaCache.cacheTime = cacheTime;
	}
}
